package com.android.pdd.baseapp.hook;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.lg.meng.MessageEvent;
import com.lg.meng.base.BaseMvpPresenter;
import com.lg.meng.utils.MvpInjectHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public abstract class BaseMvpFragment extends Fragment {
    protected Activity activity;
    public List<BaseMvpPresenter> presenterList = new ArrayList<>();

    protected abstract int provideContentViewId();

    protected View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int layoutId = provideContentViewId();
        if (layoutId == 0) {
            if ((mView = provideContentView(container)) == null) {
                throw new RuntimeException("please provide a layout id !!");
            }
        } else {
            mView = inflater.inflate(layoutId, null);
        }
//        mView.setClickable(true);
        ButterKnife.bind(this, mView);
        return mView;
    }

    protected View provideContentView(ViewGroup container) {
        return null;
    }

    protected abstract void initViews();

    protected abstract void initData();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        MvpInjectHelper.inject(this);
        EventBus.getDefault().register(this);
        initViews();
        initViews(savedInstanceState);
        initData();
    }

    protected void initViews(Bundle savedInstanceState) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        exeDestory();
    }


    protected void exeDestory() {
        for (BaseMvpPresenter presenter : presenterList) {
            if (presenter != null) {
                presenter.detachView();
            }
        }
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        Log.e("=====", "----messageEvent");
    }

    protected <V extends View> V findViewById(int id) {
        return mView.findViewById(id);
    }

    /**
     * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑
     * FragmentActivity捕捉到物理返回键点击事件后会首先询问Fragment是否消费该事件
     * 如果没有Fragment消息时FragmentActivity自己才会消费该事件
     */
    public boolean onBackPressed() {
        return false;
    }

    private static final int NONE_REQUEST_CODE = -10000;

    public void jumpTo(Class clz) {
        jumpToCommon(clz, NONE_REQUEST_CODE);
    }

    public void jumpTo(Class clz, Serializable... serializables) {
        jumpToCommon(clz, NONE_REQUEST_CODE, serializables);
    }

    public void jumpToCommon(Class clz, int requestCode, Serializable... serializables) {
        Intent intent = new Intent(activity, clz);
        if (serializables != null && serializables.length > 0) {
            Bundle bundle = new Bundle();
            for (int i = 0; i < serializables.length; i++) {
                Serializable serializable = serializables[i];
                bundle.putSerializable(i + "", serializable);
            }
            intent.putExtras(bundle);
        }
        if (requestCode == NONE_REQUEST_CODE) {
            startActivity(intent);
        } else {
            startActivityForResult(intent, requestCode);
        }
    }

    protected void sendBroadcast(Intent intent) {
        activity.sendBroadcast(intent);
    }
}
